from django.db import models

class FeatureManager(models.Manager):
    def searchbyname(self,keyword):
        return self.filter(name__icontains = keyword)

class BookManager(models.Manager):
    def title_count(self, keyword):
        return self.filter(title__icontains=keyword).count()


# Create your models here.
class Feature(models.Model):
    name = models.CharField(max_length=200)

    objects = FeatureManager()

    def __str__(self):
        return self.name

class Group(models.Model):
    name = models.CharField(max_length=200)

    def __str__(self):
        return self.name

class Fitem(models.Model):
    feature = models.ForeignKey(Feature,null=True,blank=True)
    group = models.ForeignKey(Group,null=True,blank=True)
    name = models.CharField(max_length=200)       

    def __str__(self):
        return self.name

class Category(models.Model):
    name = models.CharField(max_length=200)    
    parent = models.ForeignKey('self', null=True, blank=True, default=None)

class Location(models.Model):
    city = models.CharField(max_length=50)
    state = models.CharField(max_length=50, null=True, blank=True)
    country = models.CharField(max_length=50)

    def __str__(self):
        if self.state:
            return "%s, %s, %s" % (self.city, self.state, self.country)
        else:
            return "%s, %s" % (self.city, self.country)

class Job(models.Model):
    pub_date = models.DateField()
    job_title = models.CharField(max_length=50)
    job_description = models.TextField()
    location = models.ForeignKey(Location)

    def __str__(self):
        return "%s (%s)" % (self.job_title, self.location)

class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website = models.URLField()

    def __str__(self):
        return self.name

TITLE_CHOICES = (
('MR', 'Mr.'),
('MRS', 'Mrs.'),
('MS', 'Ms.'),
)

class Author(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField()
    title = models.CharField(max_length=3, choices=TITLE_CHOICES)

    def get_full_name(self):
        return u'%s %s' % (self.first_name, self.last_name)

    def __str__(self):
        return self.get_full_name()

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()

    objects = BookManager()

    def __str__(self):
        return self.title